package com.ar.adapter;

import com.ar.studyapp.R;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

/**
 * The Class IndexAdapter.
 * @author anh
 */
public class IndexAdapter implements SpinnerAdapter {
	
	/** The inflater. */
	private LayoutInflater inflater;
	
	/** The checked. */
	private boolean checked;
	
	/** The ans id. */
	private int[] ansId;
	
	/** The quiz nums. */
	private int quizNums;
	
	/** The ans flags. */
	private boolean[] ansFlags;

	/**
	 * Instantiates a new index adapter object.
	 *
	 * @param context the context
	 * @param quizNums the quiz nums
	 * @param ansId the ans id
	 */
	public IndexAdapter(Context context, int quizNums, int[] ansId) {
		this.ansId = ansId;
		this.checked = false;
		this.quizNums = quizNums;
		this.ansFlags = null;
		inflater = LayoutInflater.from(context);
	}

	/**
	 * Sets the checked state.
	 *
	 * @param checked the new checked state
	 */
	public void setCheckedState(boolean checked) {
		this.checked = checked;
	}
	
	/**
	 * Sets the answer flags.
	 *
	 * @param ansFlags the new answer flags
	 */
	public void setAnswerFlags(boolean[] ansFlags){
		this.ansFlags = ansFlags;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		return quizNums;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int position) {
		return null;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		return 0;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemViewType(int)
	 */
	@Override
	public int getItemViewType(int position) {
		return 0;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TitleViewHolder holder = new TitleViewHolder();
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.ab_title, null);
			holder.tvTitle = (TextView) convertView
					.findViewById(R.id.tv_ab_title);
			convertView.setTag(holder);
		} else
			holder = (TitleViewHolder) convertView.getTag();
		holder.tvTitle.setText(new String("Câu " + (position + 1) + "/"
				+ getCount()));
		return convertView;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getViewTypeCount()
	 */
	@Override
	public int getViewTypeCount() {
		return 0;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#hasStableIds()
	 */
	@Override
	public boolean hasStableIds() {
		return false;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#isEmpty()
	 */
	@Override
	public boolean isEmpty() {
		return false;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#registerDataSetObserver(android.database.DataSetObserver)
	 */
	@Override
	public void registerDataSetObserver(DataSetObserver observer) {

	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#unregisterDataSetObserver(android.database.DataSetObserver)
	 */
	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {

	}

	/* (non-Javadoc)
	 * @see android.widget.SpinnerAdapter#getDropDownView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getDropDownView(int pos, View convertView, ViewGroup parent) {
		DropdownViewHolder dropdownHolder = new DropdownViewHolder();
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.ab_dropdown, null);
			dropdownHolder.tvIndex = (TextView) convertView
					.findViewById(R.id.tv_ab_dropdown);
			dropdownHolder.imageViewChecked = (ImageView) convertView
					.findViewById(R.id.imageViewChecked);
			convertView.setTag(dropdownHolder);
		} else
			dropdownHolder = (DropdownViewHolder) convertView.getTag();

		dropdownHolder.tvIndex.setText(new String("Câu " + (pos + 1)));
		if (!checked)
			if (ansId[pos] != -1)
				dropdownHolder.imageViewChecked.setVisibility(View.VISIBLE);
			else
				dropdownHolder.imageViewChecked.setVisibility(View.INVISIBLE);
		else if (ansFlags[pos] == true) {
			dropdownHolder.imageViewChecked.setImageResource(R.drawable.ic_action_done);
			dropdownHolder.imageViewChecked.setVisibility(View.VISIBLE);
			}
		else
			dropdownHolder.imageViewChecked.setVisibility(View.INVISIBLE);
		return convertView;
	}

	/**
	 * The Class TitleViewHolder.
	 */
	class TitleViewHolder {
		
		/** The tv title. */
		TextView tvTitle;
	}

	/**
	 * The Class DropdownViewHolder.
	 * @author anh
	 */
	class DropdownViewHolder {
		
		/** The tv index. */
		TextView tvIndex;
		
		/** The image view checked. */
		ImageView imageViewChecked;
	}

}
